
/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2020原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.beans.factory;

import org.springframework.lang.Nullable;

/**
 * Interface to be implemented by objects used within a {@link BeanFactory} which
 * are themselves factories for individual objects. If a bean implements this
 * interface, it is used as a factory for an object to expose, not directly as a
 * bean instance that will be exposed itself.
 *
 * <p><b>NB: A bean that implements this interface cannot be used as a normal bean.</b>
 * A FactoryBean is defined in a bean style, but the object exposed for bean
 * references ({@link #getObject()}) is always the object that it creates.
 *
 * <p>FactoryBeans can support singletons and prototypes, and can either create
 * objects lazily on demand or eagerly on startup. The {@link SmartFactoryBean}
 * interface allows for exposing more fine-grained behavioral metadata.
 *
 * <p>This interface is heavily used within the framework itself, for example for
 * the AOP {@link org.springframework.aop.framework.ProxyFactoryBean} or the
 * {@link org.springframework.jndi.JndiObjectFactoryBean}. It can be used for
 * custom components as well; however, this is only common for infrastructure code.
 *
 * <p><b>{@code FactoryBean} is a programmatic contract. Implementations are not
 * supposed to rely on annotation-driven injection or other reflective facilities.</b>
 * {@link #getObjectType()} {@link #getObject()} invocations may arrive early in the
 * bootstrap process, even ahead of any post-processor setup. If you need access to
 * other beans, implement {@link BeanFactoryAware} and obtain them programmatically.
 *
 * <p><b>The container is only responsible for managing the lifecycle of the FactoryBean
 * instance, not the lifecycle of the objects created by the FactoryBean.</b> Therefore,
 * a destroy method on an exposed bean object (such as {@link java.io.Closeable#close()})
 * will <i>not</i> be called automatically. Instead, a FactoryBean should implement
 * {@link DisposableBean} and delegate any such close call to the underlying object.
 *
 * <p>Finally, FactoryBean objects participate in the containing BeanFactory's
 * synchronization of bean creation. There is usually no need for internal
 * synchronization other than for purposes of lazy initialization within the
 * FactoryBean itself (or the like).
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @since 08.03.2003
 * @param <T> the bean type
 * @see org.springframework.beans.factory.BeanFactory
 * @see org.springframework.aop.framework.ProxyFactoryBean
 * @see org.springframework.jndi.JndiObjectFactoryBean
 */
/**
 *要由｛@link BeanFactory｝中使用的对象实现的接口
 *它们本身就是单个对象的工厂。如果bean实现了
 *接口，它用作要公开的对象的工厂，而不是直接作为
 *将自己公开的bean实例。
 *
 *＜p＞＜b＞注意：实现此接口的bean不能用作普通bean</b>
 *FactoryBean是在bean样式中定义的，但为bean公开的对象
 *references（｛@link#getObject（）｝）始终是它创建的对象。
 *
 *＜p＞FactoryBean可以支持单件和原型，并且可以创建
 *对象在需要时慵懒地或在启动时急切地。｛@link SmartFactoryBean｝
 *接口允许公开更细粒度的行为元数据。
 *
 *＜p＞此接口在框架本身中大量使用，例如
 *AOP｛@linkorg.springframework.AOP.framework.ProxyFactoryBean｝或
 *｛@link.org.springframework.jndi.JndiObjectFactoryBean｝。它可以用于
 *定制组件；然而，这仅适用于基础结构代码。
 *
 *＜p＞＜b＞｛@code FactoryBean｝是一个编程合约。实施不是
 *应该依赖注释驱动的注入或其他反射设施</b>
 *｛@link#getObjectType（）｝｛@link#getObject（）｝｝调用可能在
 *引导过程，甚至在任何后处理器设置之前。如果您需要访问
 *其他bean，实现｛@link BeanFactoryAware｝并以编程方式获取它们。
 *
 *＜p＞＜b＞容器只负责管理FactoryBean的生命周期
 *实例，而不是FactoryBean创建的对象的生命周期</b> 因此，
 *暴露的bean对象上的destroy方法（例如｛@linkjava.io.Closable#close（）｝）
 *将不会自动调用<i></i>。相反，FactoryBean应该实现
 *｛@link DisposableBean｝并将任何此类关闭调用委托给基础对象。
 *
 *＜p＞最后，FactoryBean对象参与包含BeanFactory的
 *bean创建的同步。通常不需要内部
 *除了用于
 *FactoryBean本身（或类似）。
 *
 *@作者Rod Johnson
 *@作者Juergen Hoeller
 *@自2003年3月8日
 *@param＜T＞bean类型
 *@参见org.springframework.beans.factory.BeanFactory
 *@参见org.springframework.aop.framework.ProxyFactoryBean
 *@参见org.springframework.jndi.JndiObjectFactoryBean
 */
public interface FactoryBean<T> {

	/**
	 * The name of an attribute that can be
	 * {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
	 * {@link org.springframework.beans.factory.config.BeanDefinition} so that
	 * factory beans can signal their object type when it can't be deduced from
	 * the factory bean class.
	 * @since 5.2
	 */
	/**
	 *属性的名称
	 *上的｛@link org.springframework.core.AttributeAccessor#setAttribute集合｝
	 *{@link.org.springframework.beans.factory.config.BeanDefinition}，以便
	 *工厂bean可以在无法从中推断对象类型时发出信号
	 *工厂bean类。
	 *@自5.2
	 */
	String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";


	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * <p>As with a {@link BeanFactory}, this allows support for both the
	 * Singleton and Prototype design pattern.
	 * <p>If this FactoryBean is not fully initialized yet at the time of
	 * the call (for example because it is involved in a circular reference),
	 * throw a corresponding {@link FactoryBeanNotInitializedException}.
	 * <p>As of Spring 2.0, FactoryBeans are allowed to return {@code null}
	 * objects. The factory will consider this as normal value to be used; it
	 * will not throw a FactoryBeanNotInitializedException in this case anymore.
	 * FactoryBean implementations are encouraged to throw
	 * FactoryBeanNotInitializedException themselves now, as appropriate.
	 * @return an instance of the bean (can be {@code null})
	 * @throws Exception in case of creation errors
	 * @see FactoryBeanNotInitializedException
	 */
	/**
	 *返回对象的实例（可能是共享的或独立的）
	 *由这家工厂管理。
	 *＜p＞与｛@link BeanFactory｝一样，这允许支持
	 *Singleton和原型设计模式。
	 *＜p＞如果此FactoryBean在
	 *调用（例如，因为它涉及循环引用），
	 *引发相应的｛@link FactoryBeanNotInitializedException｝。
	 *＜p＞从Spring 2.0开始，FactoryBean允许返回｛@code null｝
	 *对象。工厂将此视为正常使用值；它
	 *在这种情况下，将不再引发FactoryBeanNotInitializedException。
	 *鼓励FactoryBean实现抛出
	 *FactoryBeanNotInitializedException本身（视情况而定）。
	 *@return bean的实例（可以是｛@code null｝）
	 *创建错误时@throws异常
	 *@see FactoryBean未初始化异常
	 */
	@Nullable
	T getObject() throws Exception;

	/**
	 * Return the type of object that this FactoryBean creates,
	 * or {@code null} if not known in advance.
	 * <p>This allows one to check for specific types of beans without
	 * instantiating objects, for example on autowiring.
	 * <p>In the case of implementations that are creating a singleton object,
	 * this method should try to avoid singleton creation as far as possible;
	 * it should rather estimate the type in advance.
	 * For prototypes, returning a meaningful type here is advisable too.
	 * <p>This method can be called <i>before</i> this FactoryBean has
	 * been fully initialized. It must not rely on state created during
	 * initialization; of course, it can still use such state if available.
	 * <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return
	 * {@code null} here. Therefore, it is highly recommended to implement
	 * this method properly, using the current state of the FactoryBean.
	 * @return the type of object that this FactoryBean creates,
	 * or {@code null} if not known at the time of the call
	 * @see ListableBeanFactory#getBeansOfType
	 */
	/**
	 *返回此FactoryBean创建的对象类型，
	 *或｛@code null｝（如果事先不知道）。
	 *＜p＞这允许您检查特定类型的bean，而无需
	 *实例化对象，例如在自动布线上。
	 *＜p＞在创建单例对象的实现中，
	 *该方法应尽量避免单例创建；
	 *它应该提前估计类型。
	 *对于原型，在这里返回有意义的类型也是可取的。
	 *＜p＞这个方法可以在这个FactoryBean的＜/i＞之前调用＜i＞
	 *已完全初始化。它不能依赖于在
	 *初始化；当然，如果可用，它仍然可以使用这种状态。
	 *<p><b>注意：</b>自动连线将忽略返回的FactoryBean
	 *此处为｛@code null｝。因此，强烈建议实施
	 *该方法使用FactoryBean的当前状态。
	 *@返回此FactoryBean创建的对象类型，
	 *或｛@code null｝（如果在调用时不知道）
	 *@参见ListableBeanFactory#getBeanOfType
	 */
	@Nullable
	Class<?> getObjectType();

	/**
	 * Is the object managed by this factory a singleton? That is,
	 * will {@link #getObject()} always return the same object
	 * (a reference that can be cached)?
	 * <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object,
	 * the object returned from {@code getObject()} might get cached
	 * by the owning BeanFactory. Hence, do not return {@code true}
	 * unless the FactoryBean always exposes the same reference.
	 * <p>The singleton status of the FactoryBean itself will generally
	 * be provided by the owning BeanFactory; usually, it has to be
	 * defined as singleton there.
	 * <p><b>NOTE:</b> This method returning {@code false} does not
	 * necessarily indicate that returned objects are independent instances.
	 * An implementation of the extended {@link SmartFactoryBean} interface
	 * may explicitly indicate independent instances through its
	 * {@link SmartFactoryBean#isPrototype()} method. Plain {@link FactoryBean}
	 * implementations which do not implement this extended interface are
	 * simply assumed to always return independent instances if the
	 * {@code isSingleton()} implementation returns {@code false}.
	 * <p>The default implementation returns {@code true}, since a
	 * {@code FactoryBean} typically manages a singleton instance.
	 * @return whether the exposed object is a singleton
	 * @see #getObject()
	 * @see SmartFactoryBean#isPrototype()
	 */
	/**
	 *这个工厂管理的对象是单例的吗？即，
	 *｛@link#getObject（）｝将始终返回相同的对象
	 *（可以缓存的引用）？
	 *<p><b>注意：</b>如果FactoryBean指示保存单例对象，
	 *从｛@code getObject（）｝返回的对象可能会被缓存
	 *由拥有的BeanFactory提供。因此，不要返回｛@code true｝
	 *除非FactoryBean始终公开相同的引用。
	 *＜p＞FactoryBean本身的单例状态通常
	 *由拥有的BeanFactory提供；通常，它必须是
	 *在那里定义为单例。
	 *<p><b>注意：</b>此方法返回｛@code false｝
	 *必须指示返回的对象是独立的实例。
	 *扩展｛@link SmartFactoryBean｝接口的实现
	 *可以通过其
	 *｛@link SmartFactoryBean#isPrototype（）｝方法。普通｛@link FactoryBean｝
	 *不实现此扩展接口的实现有
	 *如果
	 *｛@codeisSingleton（）｝实现返回｛@code false｝。
	 *＜p＞默认实现返回｛@code true｝，因为
	 *｛@code FactoryBean｝通常管理单个实例。
	 *@return暴露的对象是否为单例
	 *@参见#getObject（）
	 *@see SmartFactoryBean#isPrototype（）
	 */
	default boolean isSingleton() {
		return true;
	}

}
